package com.my12306.common.util.quartz.scheduler;

import com.my12306.common.exception.RTExcption;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.TriggerKey;

import java.util.List;

/**
 * @Author:苏牧夕
 * @Date:2020/5/29 2:48
 * @Version 1.0
 */
public abstract class AbstractScheduler implements BaseScheduler {
    @Override
    public void deleteAllJob(Scheduler scheduler, List<JobKey> jobKeys) {
        try {
            scheduler.deleteJobs(jobKeys);
        } catch (SchedulerException e) {
            new RTExcption("批量删除失败！", e);
        }
    }

    /**
     * 暂停定时任务
     *
     * @param scheduler
     * @param jobKey
     */
    @Override
    public void pauseJob(Scheduler scheduler, JobKey jobKey) {
        try {
            scheduler.pauseJob(jobKey);
        } catch (SchedulerException e) {
            try {
                throw new SchedulerException("暂停任务失败！", e);
            } catch (SchedulerException ex) {
                throw new RuntimeException("系统暂停任务失败！", ex);
            }
        }
    }

    /**
     * 暂停所有定时任务
     *
     * @param scheduler
     */
    @Override
    public void pauseAllJob(Scheduler scheduler) {
        try {
            scheduler.pauseAll();
        } catch (SchedulerException e) {
            try {
                throw new SchedulerException("暂停所有定时任务！", e);
            } catch (SchedulerException ex) {
                throw new RuntimeException("暂停所有任务系统异常", ex);
            }
        }
    }

    /**
     * 根据定时任务的Jobkey唤醒任务
     *
     * @param scheduler
     * @param jobKey
     */
    @Override
    public void resumeJob(Scheduler scheduler, JobKey jobKey) {
        try {
            System.out.println(jobKey);
            System.out.println(jobKey.getClass());
            System.out.println(jobKey.getName());
            System.out.println(jobKey.getGroup());
            scheduler.resumeJob(jobKey);
        } catch (SchedulerException e) {
            try {
                throw new SchedulerException("唤醒任务失败！", e);
            } catch (SchedulerException ex) {
                throw new RTExcption("系统唤醒任务异常！", ex);
            }
        }
    }

    /**
     * 唤醒所有被暂停的定时任务
     */
    @Override
    public void resumeAllJob(Scheduler scheduler) {
        try {
            scheduler.resumeAll();
        } catch (SchedulerException e) {
            try {
                throw new SchedulerException("唤醒所有任务失败！", e);
            } catch (SchedulerException ex) {
                throw new RTExcption("唤醒所有任务系统异常", ex);
            }
        }
    }

    /**
     * 根据jobKey删除定时任务
     *
     * @param scheduler 调度器
     * @param jobKey    定时任务的jobkey
     */
    @Override
    public void deleteJob(Scheduler scheduler, JobKey jobKey, TriggerKey triggerKey) {
        try {
            scheduler.pauseTrigger(triggerKey);
            scheduler.unscheduleJob(triggerKey);
            scheduler.deleteJob(jobKey);

        } catch (SchedulerException e) {
            try {
                throw new SchedulerException("删除任务失败!", e);
            } catch (SchedulerException ex) {
                throw new RTExcption("删除定时任务失败", ex);
            }
        }
    }


}
